package com.example.application.views.login;

import com.vaadin.flow.component.UI;
import com.vaadin.flow.component.html.Div;
import com.vaadin.flow.component.login.LoginForm;
import com.vaadin.flow.component.orderedlayout.FlexComponent;
import com.vaadin.flow.component.orderedlayout.HorizontalLayout;
import com.vaadin.flow.component.orderedlayout.VerticalLayout;
import com.vaadin.flow.router.HighlightAction;
import com.vaadin.flow.router.Route;

@Route("login")
public class LoginView extends Div {
    public LoginView(){
        //创建一个水平布局
        HorizontalLayout layout = new HorizontalLayout();
        //设置距离上面300px
        layout.getStyle().set("margin-top","100px");
        //创建一个登录窗口
        LoginForm component = new LoginForm();
        //开始登录操作
        component.setEnabled(true);
        //关闭忘记密码
        component.setForgotPasswordButtonVisible(false);
        //添加登录事件
        component.addLoginListener(e->{
            //System.out.println("aaaaa");
            //获得输入框的值
            String username =  e.getUsername();
            String pwd = e.getPassword();
            if("abc".equals(username) &&"abc123".equals(pwd) ){
                //跳转页面
                UI.getCurrent().navigate("study");
            }else{
                //UI.getCurrent().navigate("login");
                //允许你使用js
                UI.getCurrent().getPage().executeJs("alert('用户名或则密码错误');window.location.href='login'");

            }
        });

         //让窗体居中
        layout.setJustifyContentMode(FlexComponent.JustifyContentMode.CENTER);
        //把登录窗体放进水平布局中
        layout.add(component);
        //在水平布局添加到容器中
        this.add(layout);
    }

}
